<?php
namespace Home\Controller;
use Common\Controller\BaseController;

class IndexController extends BaseController {
	protected function _initialize(){
		parent::_initialize();
		//导航栏
    	$classify_list = M('classify')->field('id,name,link')->where('status=1 and pid=0')->order('sort desc')->limit(6)->select();
    	$this->assign('classify_list', $classify_list);
	}
	
	public function index() {
    	//轮播图
    	$slide_list = M('slide')->field('cid,pic,link')->where('status=1')->order('sort desc')->select();
    	$this->assign('slide_list', $slide_list);
    	//新闻列表
    	$info = array(array('id' => '11', 'num' => 2), array('id' => '5', 'num' => 2));
    	$news_list = array();
    	foreach($info as $v){
	    	$list = M('news')->field('id,pid,title,times,desc')->where('status=1 and pid in (' . $v['id'] . ')')->order('sort desc')->limit($v['num'])->select();
	    	$news_list = array_merge($news_list, $list);
    	}
    	$this->assign('news_list', $news_list);
        $this->display();
	}
	
	public function lists(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取当前分类信息
			$classify_info = $this->getInfo('id,pid,name,subname,link', 'classify', 'id=' . $id);
			$this->assign('top_pid', $classify_info['pid'] > 0 ? $classify_info['pid'] : $id);
			if ($classify_info){
				$this->assign('classify_info', $classify_info);
				//获取分类及子分类信息
				$pid = $classify_info['pid'] > 0 ? $classify_info['pid'] : $id;
				$classify_list_sub = $this->getList('id,pid,name,subname,link', 'classify', 'status=1 and (id=' . $pid . ' or pid=' . $pid . ')', 'sort desc');
				$this->assign('classify_list_sub', $classify_list_sub);
				//获取当前分类及子分类新闻
				if($classify_info['pid'] == 0){
					foreach ($classify_list_sub as $key => $value) {
						$ids[] = $value['id'];
					}
				}else{
					$ids[] = $id;
				}
				$news_list = $this->getList('id,pid,title,times,view,desc', 'news', 'status=1 and pid in (' . implode(',', $ids) . ')', 'sort desc,times desc', true);
				$this->assign('news_list', $news_list);
				$this->display();
			}
		}
	}

	public function reserch(){
		$keyword = I('get.keyword', 0, 'strval');
		if($keyword){
			$this->assign('classify_info', array('pid' => 0, 'name' => '信息搜索', 'subname' => 'RESERCH'));
			$this->assign('classify_list_sub', array(array('pid' => 0, 'name' => '信息搜索', 'subname' => 'RESERCH')));
			//获取当前分类新闻
			$news_list = $this->getList('id,title,times,view,desc', 'news', 'status=1 and (title like "%' . $keyword . '%" or content like "%' . $keyword . '%")', 'sort desc,times desc', true);
			$this->assign('news_list', $news_list);
			$this->display('lists');
		}
	}
	
	public function news(){
		$id = I('get.id', 0, 'intval');
		if($id > 0){
			//获取当前新闻信息
			$news_info = $this->getInfo('id,pid,title,times,view,content,type', 'news', 'status=1 and id=' . $id);
			if($news_info){
				//更新浏览数
				M('news')->where(array('id' => $id))->setInc('view');
				if($news_info['type'] == 1){
					preg_match_all('/<img.*?src="(.*?)".*?alt="(.*?)".*?>/is', $news_info['content'], $news_info['img']);
				}
				$this->assign('news_info', $news_info);
				//获取分类及子分类信息
				$classify_info = $this->getInfo('id,pid,name,subname,link', 'classify', 'id=' . $news_info['pid']);
				$this->assign('top_pid', $classify_info['pid'] > 0 ? $classify_info['pid'] : $classify_info['id']);
				$this->assign('classify_info', $classify_info);
				$id = $classify_info['pid'] > 0 ? $classify_info['pid'] : $classify_info['id'];
				$classify_list_sub = $this->getList('id,pid,name,subname,link', 'classify', 'status=1 and (id=' . $id . ' or pid=' . $id . ')', 'sort desc');
				$this->assign('classify_list_sub', $classify_list_sub ? $classify_list_sub : array($classify_info));
				$this->display();
			}
		}
	}

	public function message(){
		if(IS_POST){
			$_POST['times'] = time();
			$result = $this->updateData($_POST, 'message');
			$this->returnResult($result);
		}else{
			$list = M('classify')->where('pid=4')->select();
			$this->assign('list', $list);
			$this->display();
		}
	}
}